from app.models import BaseModel
from app.models.userModel import User
from tools import db
from utils.enums import IntegerChoices

# 赛题与虚拟机多对多绑定关系
vm_bind_questions = db.Table(
    "api_vm_questions",
    db.Column("id", db.Integer, primary_key=True),
    db.Column(
        "vm_id",
        db.Integer,
        db.ForeignKey("api_range_vm.id", ondelete="CASCADE", onupdate="CASCADE"),
    ),
    db.Column(
        "question_id",
        db.Integer,
        db.ForeignKey("api_ctf_question.id", ondelete="CASCADE", onupdate="CASCADE"),
    ),
)


class CtfIssueType(IntegerChoices):
    misc = 1, "MISC(杂项)"
    crypto = 2, "CRYPTO(加密)"
    pwn = 3, "PWN(二进制漏洞利用)"
    reverse = 4, "Reverse(逆向工程)"
    industrial_control = 5, "工控"


class CtfIssueBelong(IntegerChoices):
    system = 1, "系统赛题"
    user_upload = 2, "用户上传"
    system_bind_vm = 3, "系统内置虚拟机绑定赛题"
    user_bind_vm = 4, "拓扑页面用户自定义保存的赛题"


class CtfQuestion(db.Model, BaseModel):
    __tablename__ = "api_ctf_question"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, db.ForeignKey(User.id))
    name = db.Column(db.String(120), index=True, nullable=False, comment="赛题名称")
    # 1、MISC(杂项)，2、CRYPTO(加密)，3、PWN(二进制漏洞利用)，4、Reverse(逆向工程)，5、工控
    type = db.Column(db.Integer, index=True, default="1", comment="赛题类型")
    score = db.Column(db.Integer, nullable=False)
    answer = db.Column(db.TEXT)
    describe = db.Column(db.TEXT)
    filepath = db.Column(db.String(300))
    zipname = db.Column(db.String(300))
    is_system = db.Column(db.Boolean, index=True, default=0)
    # 是否为系统赛题，0：否，1：是

    belong = db.Column(db.Integer, default=CtfIssueBelong.user_upload, comment="赛题归属类型")
    # 赛题关联绑定的虚拟机
    bind_vm = db.relationship(
        "RangeVm", secondary=vm_bind_questions, backref="bind_question", lazy="dynamic"
    )

    def to_dict(self):
        return {
            "id": self.id,
            "name": self.name,
            "type": self.type,
            "score": self.score,
            "describe": self.describe,
            "filepath": self.filepath,
            "is_system": self.is_system,
            "zipname": self.zipname,
        }

    def to_init_dict(self):
        return {
            "id": self.id,
            "user_id": self.user_id,
            "name": self.name,
            "type": self.type,
            "score": self.score,
            "answer": self.answer,
            "describe": self.describe,
            "filepath": self.filepath,
            "is_system": self.is_system,
            "zipname": self.zipname
        }


class IssueInitialBindRelation(db.Model, BaseModel):
    """场景克隆初始时，系统赛题与虚拟机绑定关系记录"""

    __tablename__ = "api_issue_initial_bind_vm_relation"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    range_uuid = db.Column(db.String(40), index=True, comment="场景 uuid ID")
    vm_uuid = db.Column(db.String(40), index=True, comment="虚拟机ID")
    issue_id = db.Column(db.Integer, index=True, comment="赛题ID")
